package com.sgu6.sgu6_machine.ui.fragment.set;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.dto.BarnStockParamsDto;
import com.sgu6.sgu6_machine.presenter.MachineP;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 谷仓设置
 */
public class GarnerF extends BaseF {

    @BindView(R.id.et_a_garner)
    EditText etAGarner;
    @BindView(R.id.et_b_garner)
    EditText etBGarner;

    private MachineP machineP = new MachineP();

    public static GarnerF newInstance() {
        return new GarnerF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_garner, container, false);
        unbinder = ButterKnife.bind(this, v);

        initUI();

        return v;
    }

    public void initUI() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.btn_add_a, R.id.btn_decrease_a, R.id.btn_add_b, R.id.btn_decrease_b})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_a:
                String strAnum = etAGarner.getText().toString().trim();
                try {
                    if (strAnum.length() > 0) {
                        machineP.alterBarnStock(new BarnStockParamsDto(MachineContainer.machineConfigDto.machineNo, 1, Integer.parseInt(strAnum), false));
                        Toast.makeText(requireContext(), "操作成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(requireContext(), "输入增加数量", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_decrease_a:
                String strAnum_ = etAGarner.getText().toString().trim();
                try {
                    if (strAnum_.length() > 0) {
                        machineP.alterBarnStock(new BarnStockParamsDto(MachineContainer.machineConfigDto.machineNo, 1, -Integer.parseInt(strAnum_), false));
                        Toast.makeText(requireContext(), "操作成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(requireContext(), "输入减少数量", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_add_b:
                String strBnum = etBGarner.getText().toString().trim();
                try {
                    if (strBnum.length() > 0) {
                        machineP.alterBarnStock(new BarnStockParamsDto(MachineContainer.machineConfigDto.machineNo, 2, Integer.parseInt(strBnum), false));
                        Toast.makeText(requireContext(), "操作成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(requireContext(), "输入增加数量", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_decrease_b:
                String strBnum_ = etBGarner.getText().toString().trim();
                try {
                    if (strBnum_.length() > 0) {
                        machineP.alterBarnStock(new BarnStockParamsDto(MachineContainer.machineConfigDto.machineNo, 2, -Integer.parseInt(strBnum_), false));
                        Toast.makeText(requireContext(), "操作成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(requireContext(), "输入减少数量", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
